﻿

#region using namespace
using System.Collections.Generic;
using System.Linq;
using DTO;
#endregion

namespace DAO
{
    public class PhuongDAO
    {

        // tim phuong dua vao ma phuong.
        public PhuongDTO Search(int id)
        {
            PhuongDTO phuongDTO = null;
            List<PhuongDTO> lstphuongDTO = getAllPhuong();

            foreach (PhuongDTO phuong in lstphuongDTO)
            {
                if (phuong.MaPhuong == id)
                {
                    phuongDTO = new PhuongDTO();

                    phuongDTO.MaPhuong = phuong.MaPhuong;
                    phuongDTO.TenPhuong = phuong.TenPhuong;
                    break;
                }
            }

            return phuongDTO;
        }

        //lay tat ca cac thuoc tinh cua bang phuong.
        public static List<PhuongDTO> getAllPhuong()
        {
            IList<PHUONG> ilstphuong;
            List<PhuongDTO> lstphuong = new List<PhuongDTO>();
            PhuongDTO phuongDTO = null;

            using (var context = new DataContext(GlobalDatabase.ConnectString))
            {
                ilstphuong = (from ph in context.PHUONGs select ph).ToList();
            }

            for (int i = 0; i < ilstphuong.Count; i++)
            {
                phuongDTO = new PhuongDTO(); 

                phuongDTO.MaPhuong = ilstphuong[i].MaPhuong;
                phuongDTO.TenPhuong = ilstphuong[i].TenPhuong;

                lstphuong.Add(phuongDTO);
            }

            return lstphuong;
        }
    }
}
